#include<sys/epoll.h>

#include<iostream>

#include"nocopy.hpp"

class Epoller : public nocopy
{
public:
    Epoller()
    {
        _ep_fd = epoll_create(128);
        if(_ep_fd < 0)
        {
            perror("epoll_create");
        }
    }

    int Epoller_wait(epoll_event* e_arr, int max_e_arr)
    {
        return epoll_wait(_ep_fd, e_arr, max_e_arr, 3000);
    }

    int Epoller_Update(int fd, int op, uint32_t event)
    {
        int n;
        if(op == EPOLL_CTL_DEL)
        {
            n = epoll_ctl(_ep_fd, op, fd, nullptr);
            if(n < 0)
            {
                perror("epoll_ctl");
            }
        }
        else
        {
            epoll_event ee;
            ee.events = event;
            ee.data.fd = fd;
            n = epoll_ctl(_ep_fd, op, fd, &ee);
            if(n < 0)
            {
                perror("epoll_ctl");
            }
        }
        return n;
    }

    ~Epoller()
    {
        if(_ep_fd >= 0) close(_ep_fd);
    }
private:
    int _ep_fd;
};